package com.study.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadWaitAndNotify {
    private static Object objectLock = new Object();

    public static void main(String[] args) throws InterruptedException {
//        objectMethodWaitAndNotify();
//        lockAwaitAndSignal();
        lockSportParkAndUnPark();
    }

    private static void lockSportParkAndUnPark() {

        Thread t1 = new Thread(() -> {
            System.out.println("t1 wait ");
            //加入阻塞队列
            LockSupport.park();
            System.out.println("t1 end wait and ended ");

        });
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread t2 = new Thread(() -> {
            System.out.println("t2 come in  ");
            //加入阻塞队列
            LockSupport.unpark(t1);
            System.out.println("t2 ended ");
        });
        t2.start();
    }

    private static void lockAwaitAndSignal() throws InterruptedException {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("t1 start ");
                try {
                    System.out.println("t1 wait ");
                    //加入阻塞队列
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t1 end wait and ended ");
            } finally {
                lock.unlock();
            }
        });
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        Thread t2 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("t2 start ");
                System.out.println("t2 notify ");
                //唤醒阻塞队列中的线程
                condition.signalAll();
                System.out.println("t2 end wait and notify ");
            } finally {
                lock.unlock();
            }
        });
        t2.start();
    }

    private static void objectMethodWaitAndNotify() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (objectLock) {
                System.out.println("t1 start ");
                try {
                    System.out.println("t1 wait ");
                    //加入阻塞队列
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t1 end wait and ended ");
            }
        });
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        Thread t2 = new Thread(() -> {
            synchronized (objectLock) {
                System.out.println("t2 start ");
                System.out.println("t2 notify ");
                //唤醒阻塞队列中的线程
                objectLock.notifyAll();
                System.out.println("t2 end wait and notify ");
            }
        });
        t2.start();
    }
}
